package com.fenfen.novel.utils;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.fenfen.novel.R;
import com.fenfen.novel.adapter.ChapterAdapter;
import com.fenfen.novel.model.Chapter;
import com.fenfen.novel.rest.RestClient;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChapterListUtil {
  private static String Tag = ChapterListUtil.class.getSimpleName();

  private Context mContext;

  public ChapterListUtil(Context context) {
    this.mContext = context;
  }

  public void initChaptersRecyclerView(RecyclerView recyclerView, boolean hasLoadingAll, ChapterAdapter adapter) {
    //布局
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
    mLayoutManager.setAutoMeasureEnabled(true);
    mLayoutManager.setSmoothScrollbarEnabled(true);
    recyclerView.setLayoutManager(mLayoutManager);
    //添加分割线
    recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
        .color(R.color.md_grey_500).size(1).margin(0).showLastDivider().positionInsideItem(true).build());
    //preListAdapter
    adapter.setItemClickListener((ChapterAdapter.ChapterClickListener) mContext);
    adapter.setHasLoadingAll(hasLoadingAll);
    recyclerView.setAdapter(adapter);
    //scroll
    if (hasLoadingAll) {
      //动画
//      recyclerView.setItemAnimator(new FadeInAnimator());
//      recyclerView.getItemAnimator().setAddDuration(500);
//      recyclerView.getItemAnimator().setRemoveDuration(500);
      //scroll
      recyclerView.setNestedScrollingEnabled(false);
    }
  }

  public void loadNovelPreChapters(int novelId, final ChapterAdapter adapter, final CommonUtil.CallbackListener listener) {
//    int page = preListAdapter.getItemCount() / CommonUtil.PAGE_SIZE + 1;
    Call<List<Chapter>> call = RestClient.newInstance().getNovelService().showNovelChaptersByPage(novelId, CommonUtil.FIRST_PAGE);
    call.enqueue(new Callback<List<Chapter>>() {
      @Override
      public void onResponse(Call<List<Chapter>> call, Response<List<Chapter>> response) {
        listener.onResponse();
        List<Chapter> list = response.body();
        if (list == null || list.size() == 0) {
          return;
        }
        if (adapter != null) {
          adapter.addData(list);
        }
      }

      @Override
      public void onFailure(Call<List<Chapter>> call, Throwable t) {
//        isLoadingAll = false;
        if(mContext == null) {
          return;
        }
        listener.onFailure();
      }
    });
  }

  public void loadNovelAllChapters(int novelId, final ChapterAdapter adapter) {
//    int page = preListAdapter.getItemCount() / CommonUtil.PAGE_SIZE + 1;
    Call<List<Chapter>> call = RestClient.newInstance().getNovelService().showNovelChaptersByPage(novelId, CommonUtil.FIRST_PAGE);
    call.enqueue(new Callback<List<Chapter>>() {
      @Override
      public void onResponse(Call<List<Chapter>> call, Response<List<Chapter>> response) {
        List<Chapter> list = response.body();
        if (list == null || list.size() == 0) {
          return;
        }
        if (adapter != null) {
          adapter.addData(list);
        }
      }

      @Override
      public void onFailure(Call<List<Chapter>> call, Throwable t) {
//        isLoadingAll = false;
      }
    });
  }

  public void loadNovelChaptersAll(int novelId, final ChapterAdapter allListAdapter) {
    loadNovelChaptersAll(novelId, allListAdapter, null);
  }

  public void loadNovelChaptersAll(int novelId, final ChapterAdapter allListAdapter, final CommonUtil.CallbackListener listener) {
    Call<List<Chapter>> call = RestClient.newInstance().getNovelService().showNovelChaptersAll(novelId);
    call.enqueue(new Callback<List<Chapter>>() {
      @Override
      public void onResponse(Call<List<Chapter>> call, Response<List<Chapter>> response) {
        Log.d(Tag, "load data finish");
        List<Chapter> list = response.body();
        if (list == null || list.size() == 0) {
          return;
        }
        if (allListAdapter != null) {
          allListAdapter.setData(list);
        }
        if(listener != null) {
          listener.onResponse();
        }
      }

      @Override
      public void onFailure(Call<List<Chapter>> call, Throwable t) {
//        isLoadingAll = false;
        if(listener != null) {
          listener.onFailure();
        }
      }
    });
  }
}
